﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Reflection.Emit;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace BookStore
{
    public partial class FrmLogin : Form
    {
        string IP = "127.0.0.1";
        const int PORT = 2000;
        cTcp tcpClient = new cTcp();
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void LExit_Click(object sender, EventArgs e)
        {
            Exit.FrmClose();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            tcpClient.OnDataRecieved += ReceiveData;
            tcpClient.LoginStatusRecieved += ReceiveLoginResult;
            try
            {
                if (!tcpClient.Connecter(IP, PORT))
                {
                    MessageBox.Show("Error!");
                    return;
                }
            }
            catch(Exception d)
            {
                MessageBox.Show(d.Message);
            }
        }

        private void ReceiveData(string content)
        {
            var d = new data(content);
            string cmd = d.Command;
            string cnt = d.Content;
        }

        private void ReceiveLoginResult(string content)
        {
            var d = new data(content);
            string cmd = d.Command;
            string cnt = d.Content;
            if (cmd == "result")
            {
                //如果用户名与密码正确，则登陆成功
                if (cnt == "LOGIN_OK")
                {
                    Invoke(new Action(() =>
                    {
                        this.Hide();
                        Frmgl frmgl = new Frmgl(TUserName.Text);
                        frmgl.ShowDialog();
                        this.Close();
                    }
                                     ));
                }
                else if (cnt == "FAILED")
                {
                    MessageBox.Show("Wrong Username or Password");
                }
                else
                {
                    MessageBox.Show("Wrong Login status code");
                }
            }
        }
        private void BLogin_Click(object sender, EventArgs e)
        {
            TUserName.Text = TUserName.Text.Trim();
            TPassWord.Text = TPassWord.Text.Trim();
            if (Log.CheckUserNameAndPassWord(TUserName.Text, TPassWord.Text) == false)
            {
                return;
            }
            if (tcpClient.Client.Connected)
            {
                var msg = new data("login", string.Format("{0},{1}", TUserName.Text, TPassWord.Text));
                tcpClient.WriteData(msg);
            }
 
        }

       private void LRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
       {
            FrmRegister frmreg = new FrmRegister(tcpClient);
            frmreg.ShowDialog();
       }

       private void LReset_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
       {
             FrmReset frmReset = new FrmReset(tcpClient);
             frmReset.ShowDialog();
       }
    }
}
